﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Facebook;

namespace WebApp.Models
{
    public class Person
    {
        public class Ref : Person, ISearchable
        {
            public Ref(String id)
            {
                this.id = id;
            }

            public Ref(String id, String name)
            {
                this.id = id;
                this.name = name;
            }

            public void Populate(String authToken)
            {
                FacebookClient fbc = new FacebookClient(authToken);
                dynamic person = fbc.Get(id);
                Populate(person);
            }

            public void Populate(dynamic person)
            {
                id = person["id"];
                name = person["name"];
                firstName = person["first_name"];
                lastName = person["last_name"];
                try
                {
                    username = person["username"];
                }
                catch (Exception e)
                {
                }
                try
                {
                    gender = Enum.Parse(typeof(Gender), person["gender"]);
                }
                catch (Exception e)
                {
                }
            }
        }

        #region Fields

        private String id = null;
        private String name = null;
        private String firstName = null;
        private String lastName = null;
        private String username = null;
        private Gender gender = Gender.Male;

        #endregion

        #region Properties

        public String Id
        {
            get { return id; }
        }

        public String Name
        {
            get { return name; }
        }

        public String LastName
        {
            get { return lastName; }
        }

        public String Username
        {
            get { return username; }
        }

        public Gender Gender
        {
            get { return gender; }
        }

        #endregion

        #region Factory

        public class Factory : IFactory<Person.Ref>
        {
            public Person.Ref Create(String id)
            {
                return new Person.Ref(id);
            }
        }

        #endregion
    }
}